package com.xz.xzmybatisagent.socket.example;

import com.xz.xzmybatisagent.socket.*;

import java.util.stream.IntStream;

/**
 * <p>
 *
 * </p>
 *
 * @author DONG.WANG17@HAND-CHINA.COM
 * @since 2025/3/22-03-22 16:21
 */
public class ServerExample {
    public static void main(String[] args) {
        SocketConfig config = new SocketConfig(8888);
        config.setReceiveHeartbeatTag(false);
        config.setCallback(new SocketCallback() {
            int i = 1;

            @Override
            public void onMessage(String message) {
                // System.out.println("[Server] Received: " + message);
            }

            @Override
            public String onMessageAndResponse(String message) {
                System.out.println("[Server] Received: " + message + " " + i++);
                // return "我是 Server，我已收到你的消息：" + message;
                return null;
            }

            @Override
            public void onError(Exception e) {
                System.err.println("[Server] Error: " + e.getMessage());
            }

            @Override
            public void onStatusChange(ConnectionStatus status) {
                System.out.println("[Server] Status: " + status);
            }
        });
        SocketEndpoint server = SocketFactory.createServer(config);
        server.start();

        // 发送示例
        IntStream.range(0, 100).forEach(i -> {
            server.sendData("i am server " + i);
            try {
                Thread.sleep(5000);
            } catch (InterruptedException ignored) {
            }
        });
    }
}
